<?php

class BlogsController extends AppController {

    var $name = 'Blogs';
    var $helpers = array('Html', 'Form', 'Javascript', 'Fck', 'Text');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('allposts', 'commentsbypost', 'view', 'index', 'categorylist', 'postsbycategory');
    }

    function index() {
        $this->layout = 'cms';
        $this->loadModel('Category');
        $this->set('item', $this->Category->categoryArray());
        $this->Blog->recursive = 0;
        $this->paginate = array(
            'limit' => 30,
            'order' => 'Blog.id DESC'
        );
        $this->set('blogs', $this->paginate());
    }

    function categorylist() {
        $this->loadModel('BlogCategory');
        $this->Blog->Behaviors->attach('Containable');
        $this->Blog->contain('BlogCategory');
        return $this->Blog->find('all', array('fields' =>
                    array('Blog.id', 'count(Blog.blog_category_id) as totalblog', 'BlogCategory.title'),
                    'group' => 'Blog.blog_category_id'));
    }

    function postsbycategory($id=null) {
        $this->layout = 'cms';
        $this->loadModel('BlogCategory');
        $this->BlogCategory->recursive = -1;
        $this->BlogCategory->id = $id;
        $this->set('blogscat', $this->BlogCategory->read());
        $this->Blog->recursive = 0;
        $this->paginate = array(
            'conditions' => array('Blog.blog_category_id' => $id),
            'limit' => 30,
            'order' => 'Blog.id DESC'
        );
        $this->set('blogs', $this->paginate());
    }

    function view($id = null) {
        $this->layout = 'cms';
        $this->loadModel('BlogComment');
        if (!empty($this->data)) {
            $this->data['BlogComment']['user_id'] = $this->Auth->User('id');
            $this->data['BlogComment']['blog_id'] = $id;
            $this->data['BlogComment']['publish'] = 1;
            $this->data['BlogComment']['created'] = date('Y-m-d H:i:s');
            $this->data['BlogComment']['modified'] = date('Y-m-d H:i:s');
            $this->BlogComment->create();
            $this->BlogComment->save($this->data);
        }
        if (!$id) {
            $this->Session->setFlash(__('Invalid blog', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('blog', $this->Blog->read(null, $id));
        $this->set('id', $id);
    }

    function commentsbypost($id = null) {
        $this->layout = null;
        $this->loadModel('BlogComment');
        $this->BlogComment->Behaviors->attach('Containable');
        $this->BlogComment->contain('User.username');
        if (!$id) {
            $this->Session->setFlash(__('Invalid blog', true));
            $this->redirect(array('action' => 'index'));
        }
        return $this->BlogComment->find('all', array('conditions' => array('blog_id' => $id, 'publish' => 1)));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Blog->create();
            if ($this->Blog->save($this->data)) {
                $this->Session->setFlash(__('The blog has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The blog could not be saved. Please, try again.', true));
            }
        }
        $blogCategories = $this->Blog->BlogCategory->find('list');
        $users = $this->Blog->User->find('list');
        $this->set(compact('blogCategories', 'users'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid blog', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Blog->save($this->data)) {
                $this->Session->setFlash(__('The blog has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The blog could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Blog->read(null, $id);
        }
        $blogCategories = $this->Blog->BlogCategory->find('list');
        $users = $this->Blog->User->find('list');
        $this->set(compact('blogCategories', 'users'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for blog', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Blog->delete($id)) {
            $this->Session->setFlash(__('Blog deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Blog was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function backstage_index() {
        $this->layout = 'admin';
        $this->Blog->Behaviors->attach('Containable');
        $this->paginate = array(
            'contain' => array('BlogCategory'),
            'limit' => 25
        );
        $this->set('blogs', $this->paginate('Blog'));
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid blog', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('blog', $this->Blog->read(null, $id));
    }

    function backstage_add() {
        $this->layout = 'admin';
        if (!empty($this->data)) {
            $this->data['Blog']['user_id'] = $this->Auth->User('id');
            //$this->data['Blog']['blog_category_id']=1;

            $this->data['Blog']['posting_date'] = date('Y-m-d');
            $this->Blog->create();
            if ($this->Blog->save($this->data)) {
                $this->Session->setFlash(__('The post has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {

                $this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
            }
        }
        $blogCategories = $this->Blog->BlogCategory->find('list');
        $users = $this->Blog->User->find('list');
        $this->set(compact('blogCategories', 'users'));
    }

    function backstage_edit($id = null) {
        $this->layout = 'admin';
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid blog', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Blog->save($this->data)) {
                $this->Session->setFlash(__('The post has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Blog->read(null, $id);
        }
        $blogCategories = $this->Blog->BlogCategory->find('list');
        $users = $this->Blog->User->find('list');
        $this->set(compact('blogCategories', 'users'));
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for blog', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Blog->delete($id)) {
            $this->Session->setFlash(__('Post deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Post was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function backstage_comments() {
        $this->layout = 'admin';
        $this->loadModel('BlogComment');
        $this->BlogComment->Behaviors->attach('Containable');
        $this->paginate = array(
            'contain' => array('Blog'),
            'limit' => 25
        );
        $this->set('comments', $this->paginate('BlogComment'));
    }

    function active($id) {
        $this->layout = "ajax";
        $this->loadModel('BlogComment');
        $this->BlogComment->id = $id;
        $this->data['BlogComment']['publish'] = 1;
        $this->BlogComment->save($this->data);
        $this->set("id", $id);
    }

    function inactive($id) {
        $this->layout = "ajax";
        $this->loadModel('BlogComment');
        $this->BlogComment->id = $id;
        $this->data['BlogComment']['publish'] = 0;
        $this->BlogComment->save($this->data);
        $this->set("id", $id);
    }

    function allposts() {
        $this->layout = 'innerhome';
        $this->loadModel('Category');
        $this->set('item', $this->Category->categoryArray());
        $this->Blog->recursive = 0;
        $this->set('blogs', $this->paginate());
    }

}

?>